package com.qh.org.controller;

import com.qh.basic.annotation.PreAuthorize;
import com.qh.basic.util.AjaxResult;
import com.qh.basic.util.pageList;
import com.qh.org.domain.Employee;
import com.qh.org.query.EmployeeQuery;
import com.qh.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工接口类",description = "处理员工的crud操作")
@RestController//==@reponseBody+@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 查找单个
     * @param id
     * @return
     */
    //http://localhost:8080/employee/1
    @ApiOperation("查询单个的接口")
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个员工",sn = "employee:getOne")
    public Employee findOne(@PathVariable("id")Long id){
        return employeeService.loadOne(id);
    }

    /**
     * 查找所有
     * @return
     */
    //http://localhost:8080/employee
    @ApiOperation("查询所有的")
    @GetMapping
    @PreAuthorize(name = "查询单个员工",sn = "employee:getAll")
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    //http://localhost:8080/employee/1
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "员工删除",sn = "employee:delete")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或者修改
     * @param employee
     * @return
     */
    //http://localhost:8080/employee/json对象
    @ApiOperation("添加或者修改")
    @PutMapping
    @PreAuthorize(name = "保存员工",sn = "employee:save")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try{
            if (employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页
     * @param employeeQuery
     * @return
     */
    //http://localhost:8080/employee/json对象
    @ApiOperation("分页")
    @PostMapping
    @PreAuthorize(name = "员工列表",sn = "employee:list")
    public pageList pageQuery(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.pageQuery(employeeQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("接口：批量删除")
    @PatchMapping
    @PreAuthorize(name = "员工批量删除",sn = "employee:patchDel")
    public AjaxResult patch(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> reject(@PathVariable("shopId") Long shopId) {
        return employeeService.queryByShopId(shopId);
    }
}
